package com.sjme.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sjme.domain.candidate.Candidate;
import com.sjme.domain.candidate.dto.CandidateDto;
import com.sjme.service.ICandidateService;
import com.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.constant.ResultConstant.ERROR;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LY
 * @since 2025-09-27
 */
@Slf4j
@RestController
@RequestMapping("/candidate")
public class CandidateController {
    @Autowired
    private ICandidateService candidateService;

    @PostMapping("/queryCandidate")
    public Result<IPage<Candidate>> query(@RequestBody CandidateDto candidate) {
        IPage<Candidate> result = candidateService.queryCandidate(candidate);
        return Result.success(result);
    }

    @PostMapping("/queryCandidateByIds")
    public Result<List<Candidate>> queryCandidateByIds(@RequestBody List<String> candidateIdList) {
        return candidateService.queryCandidateByIds(candidateIdList);
    }

    @PostMapping("/addCandidate")
    public Result add(@RequestBody Candidate candidate) {
        try {
            candidateService.save(candidate);
        } catch (Exception e) {
            log.error("addCandidate Exception", e);
            return Result.error(ERROR, e.getMessage());
        }
        return Result.success("新增成功！");
    }

    @PostMapping("/editCandidate")
    public Result edit(@RequestBody Candidate candidate) {
        try {
            candidateService.updateById(candidate);
        } catch (Exception e) {
            log.error("editCandidate Exception", e);
            return Result.error(ERROR, e.getMessage());
        }
        return Result.success("修改成功！");
    }

    @PostMapping("/deleteCandidate")
    public Result delete(@RequestBody List<String> ids) {
        try {
            candidateService.removeByIds(ids);
        } catch (Exception e) {
            log.error("deleteCandidate Exception", e);
            return Result.error(ERROR, e.getMessage());
        }
        return Result.success("修改成功！");
    }

    @PostMapping("/queryCandidateByIdsNew")
    public List<Candidate> queryCandidateByIdsNew(@RequestBody List<String> candidateIdList) {
        List<Candidate> candidateList = null;
        try {
            candidateList = candidateService.list(Wrappers.<Candidate>lambdaQuery().in(Candidate::getId, candidateIdList));
        } catch (Exception e) {
            log.error("queryCandidateByIdsNew Exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return candidateList;
    }

    @PostMapping("/queryCandidateByCondition")
    public List<Candidate> queryCandidateByCondition(@RequestBody CandidateDto candidate) {
        List<Candidate> candidateList = null;
        try {
            candidateList = candidateService.list(Wrappers.<Candidate>lambdaQuery()
                    .like(StringUtils.isNotBlank(candidate.getName()),Candidate::getName, candidate.getName()));
        } catch (Exception e) {
            log.error("queryCandidateByCondition Exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return candidateList;
    }

}
